package com.hefeng.unit.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hefeng.common.core.utils.DateUtils;
import com.hefeng.common.core.utils.EmptyUtils;
import com.hefeng.unit.domain.GyDevice;
import com.hefeng.unit.domain.GyOrderDistribution;
import com.hefeng.unit.domain.GyUser;
import com.hefeng.unit.domain.GyUserOrder;
import com.hefeng.unit.mapper.GyDeviceMapper;
import com.hefeng.unit.mapper.GyOrderDistributionMapper;
import com.hefeng.unit.mapper.GyUserMapper;
import com.hefeng.unit.mapper.GyUserOrderMapper;
import com.hefeng.unit.service.GyUserOrderService;
import com.hefeng.unit.util.OrderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
@DS("slave")
public class GyUserOrderServiceImpl extends ServiceImpl<GyUserOrderMapper, GyUserOrder> implements GyUserOrderService {
    @Autowired
    private GyUserOrderMapper gyUserOrderMapper;
    @Autowired
    private GyDeviceMapper gyDeviceMapper;
    @Autowired
    private GyUserMapper gyUserMapper;

    @Autowired
    private GyOrderDistributionMapper gyOrderDistributionMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public GyUserOrder queryById(long id){
        return gyUserOrderMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param gyUserOrder 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<GyUserOrder> getOrderPage(GyUserOrder gyUserOrder, int current, int size){
        LambdaQueryWrapper<GyUserOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GyUserOrder::getUserId, gyUserOrder.getUserId());
        queryWrapper.eq(GyUserOrder::getIsDeleted,1);
        queryWrapper.orderByDesc(GyUserOrder::getCreatedTime);
        Page<GyUserOrder> pagin = new Page<>(current , size);
        pagin=gyUserOrderMapper.selectPage(pagin,queryWrapper);
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param gyUserOrder 实例对象
     * @return 实例对象
     */
    public Map<String,Object> insert(GyUserOrder gyUserOrder){
        Map<String,Object>map=new HashMap<>();
        if (EmptyUtils.isEmpty(gyUserOrder.getUserId())){
            map.put("make",false);
            map.put("msg","用户信息不能未空！");
            return map;
        }
        if (EmptyUtils.isEmpty(gyUserOrder.getOrderUserName())){
            map.put("make",false);
            map.put("msg","用户姓名不能未空！");
            return map;
        }
        if (EmptyUtils.isEmpty(gyUserOrder.getOrderUserMobile())){
            map.put("make",false);
            map.put("msg","用户手机号码不能未空！");
            return map;
        }
        //获取用户信息
        //GyUser user=gyUserMapper.selectById(gyUserOrder.getUserId());
        //新增工单开始
        gyUserOrder.setOrderCode(OrderUtils.getOrderCode());
        gyUserOrder.setOrderTitle(gyUserOrder.getOrderUserName()+"提交的工单");
        //默认分配用户 状态为2
        gyUserOrder.setOrderState(2);

        gyUserOrder.setIsDeleted(1);
        gyUserOrder.setCreatedTime(new Date());
        gyUserOrderMapper.insert(gyUserOrder);
        map.put("make",true);
        map.put("order", gyUserOrder);
        //创建工单派发信息
        //GyDevice device=gyDeviceMapper.selectById(gyUserOrder.getDeviceId());
        GyOrderDistribution gyOrderDistribution =new GyOrderDistribution();
        gyOrderDistribution.setWorkId(gyUserOrder.getOrderId().toString());
        gyOrderDistribution.setDeviceCode(gyUserOrder.getDeviceCode());
        gyOrderDistribution.setWorkType("2");
        gyOrderDistribution.setStatus("1");
        gyOrderDistribution.setDispatcher("127");
        gyOrderDistribution.setDispatchTime(new Date());
        gyOrderDistribution.setContactDetails("18228091362");
        gyOrderDistribution.setCreatedUser("管理员");
        gyOrderDistribution.setCreatedTime(new Date());
        gyOrderDistribution.setExpectedCompletionTime(DateUtils.addDays(new Date(),3));
        gyOrderDistributionMapper.insert(gyOrderDistribution);
        //发送工单至燃气平台
        return map;
    }

    /**
     * 更新数据
     *
     * @param gyUserOrder 实例对象
     * @return 实例对象
     */
    public GyUserOrder update(GyUserOrder gyUserOrder){
        //1. 根据条件动态更新

        return gyUserOrder;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(long id){
        int total = gyUserOrderMapper.deleteById(id);
        return total > 0;
    }
}
